Skip to content

Add actionable remediation approaches and patterns for CA2219 (Fixes #48230) - #56653

Open
Manish-1kumar wants to merge 1 commit into
dotnet:mainfrom
Manish-1kumar:users/manish/issue-48230-ca2219-alternative-approaches
Open

Manish-1kumar wants to merge 1 commit into
dotnet:mainfrom
Manish-1kumar:users/manish/issue-48230-ca2219-alternative-approaches

Conversation

@Manish-1kumar

Copy link
Copy Markdown
Contributor

Description

Resolves #48230.

In \docs/fundamentals/code-analysis/quality-rules/ca2219.md\ (CA2219: Do not raise exceptions in exception clauses), replaces the brief prohibition with concrete guidance and alternative patterns for handling cleanup operations that can fail.

Changes Included

  • Added *Catch and log cleanup exceptions inside \ inally* pattern with code sample showing how to prevent secondary exceptions from masking primary exceptions.
  • Added guidance for exception filters (\catch (...) when (...)), explaining that exceptions inside filters are silently caught and evaluate to \ alse.\
  • Added Use \Environment.FailFast\ for unrecoverable cleanup failures guidance for critical unmanaged or interop state corruption where execution cannot safely proceed.\
  • Validated with \markdownlint-cli2\ (0 issues).

@dotnetrepoman dotnetrepoman Bot added this to the November 2026 milestone Nov 1, 2026
@dotnet-policy-service dotnet-policy-service Bot added dotnet-fundamentals/svc community-contribution Indicates PR is created by someone from the .NET community. labels Nov 1, 2026
@BillWagner
BillWagner requested a balanced review from Copilot November 25, 2026 16:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The cleanup example can still mask the primary exception if logging throws and can falsely report success after required cleanup fails.

3 open issues
What changed in this PR

Expands the CA2219 documentation with practical approaches to cleanup failures, addressing the guidance gap raised in #48230.

Changes:

  • Adds a catch-and-log example for cleanup inside finally.
  • Explains exception-filter behavior and when to use Environment.FailFast.
  • Clarifies exception masking and suppression guidance, and refreshes article metadata.
File Description
docs/​fundamentals/​code-analysis/​quality-rules/​ca2219.md Adds cleanup-failure patterns and clarifies CA2219 remediation guidance.

Review effort: Balanced


### Catch and log cleanup exceptions inside `finally`

If cleanup operations—such as disposing unmanaged resources, closing streams, or calling native P/Invoke APIs—can fail, wrap those operations in an internal `try/catch` block inside the `finally` clause:
{
// Log the cleanup exception without rethrowing,
// preventing it from masking any in-flight exception.
logger.LogError(ex, "Failed to clean up resource.");
Comment on lines +47 to +48
```csharp
try
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates PR is created by someone from the .NET community. dotnet-fundamentals/svc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CA2219 (Do not use throw in finally) documentation should suggest alternative approaches

2 participants